package com.zeus.base.smscenter.common.util;
import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 *
 * 基于jdk 解析xml文件
 *
 * @author djr
 *
 */
public class XMLParser {
    private XPath path;
    private Document doc;
    private XMLParser() {
    }

    public static XMLParser getInstance() {
        return new XMLParser();
    }

    /**
     * 获取当前节点下某节点String
     *
     * @param node
     * @param expression 相对于node的路径
     * @return
     * @throws XPathExpressionException
     */
    public String getString(Object node, String expression) throws XPathExpressionException {
        return (String) path.evaluate(expression, node, XPathConstants.STRING);
    }

    /**
     * 获取当前节点值
     * @param node
     * @return
     * @throws XPathExpressionException
     */
    public String getNoteValue(Node node) throws XPathExpressionException{
        return  node.getTextContent();
    }
    /**
     * 获取当前节点某个属性值
     * @param node
     * @param expression
     * @return
     * @throws XPathExpressionException
     */
    public String getNoteAttribute(Node node, String expression) throws XPathExpressionException{
        NamedNodeMap attributes = node.getAttributes();
        Node title = attributes.getNamedItem(expression);
        if (title != null) {
            return title.getTextContent();
        }
        return  "";
    }
    /**
     * 获取子节点
     *
     * @param node
     * @param expression
     *            相对于node的路径
     * @return
     * @throws XPathExpressionException
     */
    public NodeList getList(Object node, String expression)
            throws XPathExpressionException {
        return (NodeList) path.evaluate(expression, node,
                XPathConstants.NODESET);
    }
    /**
     * 初始化
     *
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
    public void init(String xmlStr) throws ParserConfigurationException, SAXException,
            IOException {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        StringReader sr = new StringReader(xmlStr);
        InputSource is = new InputSource(sr);
        doc = builder.parse(is);
        // 初始化一个xpath对象
        path = XPathFactory.newInstance().newXPath();
    }

    /**
     * 解析菜单对象，返回所有主菜单(包含一系列子菜单)
     *
     * @return
     */
    public void parse(String xmlStr) {
        try {
            if (doc == null) {
                init(xmlStr);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public XPath getPath() {
        return path;
    }

    public void setPath(XPath path) {
        this.path = path;
    }

    public Document getDoc() {
        return doc;
    }

    public void setDoc(Document doc) {
        this.doc = doc;
    }

    public static void main(String[] args) {

        try {
            XMLParser parser =  XMLParser.getInstance();
            parser.parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><id title=\"2\">1</id></root>");
            NodeList rootList =  parser.getList(parser.getDoc(), "root/id");
            for (int i = 0; i < rootList.getLength(); i++) {
                Node root = rootList.item(i);
                System.out.println(parser.getNoteValue(root));
                //System.out.println(parser.getString(root,"id"));
                System.out.println(parser.getNoteAttribute(root,"title"));
            }
        }catch (Exception e){

        }

    }
}